from PyQt4 import QtGui,QtCore,uic

import Machine

class LinearAxisSettingsWidget(QtGui.QWidget):
    def __init__(self, axisName):
        QtGui.QWidget.__init__(self)

        uic.loadUi("ui/ui_files/LinearAxisSettingsWidget.ui", self)

        self.axis = Machine.getCurrentMachine().getAxis(axisName)

        self.velocitySpin.setValue(self.axis.getMaxVelocity())
        self.minTravelSpin.setValue(self.axis.getMinTravel())
        self.maxTravelSpin.setValue(self.axis.getMaxTravel())

    def __del__(self):
        self.axis.setMaxVelocity(self.velocitySpin.value())
        self.axis.setMinTravel(self.minTravelSpin.value())
        self.axis.setMaxTravel(self.maxTravelSpin.value())

class RotaryAxisSettingsWidget(QtGui.QWidget):
    def __init__(self, axisName):
        QtGui.QWidget.__init__(self)

        uic.loadUi("ui/ui_files/RotaryAxisSettingsWidget.ui", self)

        self.axis = Machine.getCurrentMachine().getAxis(axisName)

        self.velocitySpin.setValue(self.axis.getMaxVelocity())
        self.minTravelSpin.setValue(self.axis.getMinTravel())
        self.maxTravelSpin.setValue(self.axis.getMaxTravel())
        
        if self.axis.getAssignment() == 'a':
            self.assignmentCombo.setCurrentIndex(0)
        elif self.axis.getAssignment() == 'b':
            self.assignmentCombo.setCurrentIndex(1)
        elif self.axis.getAssignment() == 'c':
            self.assignmentCombo.setCurrentIndex(2)

        # Fixme
        self.locationCombo.setCurrentIndex(self.axis.getLocation())

        pos = self.axis.getPosition()

        self.xposEdit.setText(str(pos[0]))
        self.yposEdit.setText(str(pos[1]))
        self.zposEdit.setText(str(pos[2]))

    def __del__(self):
        self.axis.setMaxVelocity(self.velocitySpin.value())
        self.axis.setMinTravel(self.minTravelSpin.value())
        self.axis.setMaxTravel(self.maxTravelSpin.value())

        index = self.assignmentCombo.currentIndex()

        if index == 0:
            self.axis.setAssignment('a')
        elif index == 1:
            self.axis.setAssignment('b')
        elif index == 2:
            self.axis.setAssignment('c')

        self.axis.setLocation(self.locationCombo.currentIndex())

        self.axis.setPosition(int(self.xposEdit.text()),
                        int(self.yposEdit.text()),
                        int(self.zposEdit.text()))

